﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Code.Models;

namespace Code.BLL
{
    public class CommenManage<Table>:ManageBase<Table> where Table:class
    {
        
    }

    public class CommenManage
    {
        private static DateTime _LoadProductTypesTime;
        private static IEnumerable<ProductType> _ProductTypes;
        public static void LoadProductType()
        {
            var CM = new CommenManage<ProductType>();
            _LoadProductTypesTime = DateTime.Now;
            _ProductTypes = CM.GetTable<ProductType>().ToArray();
        }
        public static IEnumerable<ProductType> ProductTypes
        {
            get
            {
                if (_ProductTypes == null || _LoadProductTypesTime < DateTime.Now.AddSeconds(-60))
                    LoadProductType();

                return _ProductTypes;
            }
        }
        
        private static DateTime _LoadProductPictureTypesTime;
        private static IEnumerable<ProductPictureType> _ProductPictureTypes;
        public static void LoadProductPictureType()
        {
            var CM = new CommenManage<ProductPictureType>();
            _LoadProductPictureTypesTime = DateTime.Now;
            _ProductPictureTypes = CM.GetTable<ProductPictureType>().ToArray();
        }
        public static IEnumerable<ProductPictureType> ProductPictureTypes
        {
            get
            {
                if (_ProductPictureTypes == null || _LoadProductTypesTime < DateTime.Now.AddSeconds(-60))
                    LoadProductPictureType();

                return _ProductPictureTypes;
            }
        }

        private static DateTime _LoadOptionsTime;
        private static IDictionary<OptionKey, Option> _Options;
        public static void LoadOptions()
        {
            var CM = new CommenManage<Option>();
            _LoadOptionsTime = DateTime.Now;
            var tmp = CM.GetTable<Option>().ToArray();
            _Options = (Enum.GetValues(typeof(OptionKey)) as OptionKey[]).GroupJoin(tmp, p => p.ToString(), p => p.Key, (p1, p2) => new { Key = p1, Value = p2 })
                .SelectMany(p => p.Value.DefaultIfEmpty(new Option { Key = p.Key.ToString() }), (p1, p2) => new { Key = p1.Key, Value = p2 }).ToDictionary(p => p.Key, p => p.Value);
        }

        public static IDictionary<OptionKey, Option> Options
        {
            get
            {
                if (_Options == null || _LoadOptionsTime < DateTime.Now.AddSeconds(-60))
                    LoadOptions();

                return _Options;
            }
        }

    }
}
